package com.example.yy.recycleviewtest__318yy.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.yy.recycleviewtest__318yy.R;
import com.example.yy.recycleviewtest__318yy.adapter.MyAdapter;
import com.example.yy.recycleviewtest__318yy.bean.TcaseBean;
import com.example.yy.recycleviewtest__318yy.bean.TcaseBean;
import com.example.yy.recycleviewtest__318yy.iface.TcaseListener;
import com.example.yy.recycleviewtest__318yy.iface.TcaseListener;
import com.example.yy.recycleviewtest__318yy.model.TcaseModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示布局效果
    private MyAdapter myAdapter;//适配器
    private List<TcaseBean> list;//数据源


    TcaseListener tcaseListener=new TcaseListener() {
        @Override
        public void onResponse(List<TcaseBean> beanList) {
            list=beanList;
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();


        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
        //实例化ArticleModel，调用方法获取网络数据
        TcaseModel tcaseModel=new TcaseModel();
        tcaseModel.getResultList("tcase",2,"rv1l2tidbomann2qn86nln6ur2",tcaseListener);
    }

    private void initRecyclerView() {
        //获取recyclerView，设置属性，获取数据源，绑定
        recyclerView=(RecyclerView)findViewById(R.id.recycleview);
        //创建默认的线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item的高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建Adapter
        //list=getData();
        myAdapter=new MyAdapter(this);
        myAdapter.setList(list);
        //绑定RecyclerView和adapter
        recyclerView.setAdapter(myAdapter);
    }

}
